//
//  FoodPayTableViewController.m
//  waiMai
//
//  Created by LONG on 16/4/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "FoodPayTableViewController.h"
#import "ShoppingTableViewCell.h"
#import "ZHPickView.h"
#import "RemarkViewController.h"

@interface FoodPayTableViewController ()<UITableViewDelegate,UITableViewDataSource,ZHPickViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)ZHPickView *pickview;

@end

@implementation FoodPayTableViewController{
    NSInteger      z;
    UILabel        *_monlab;
    NSString      *_timestr;
    NSString      *_remarklab;//备注


}
- (UITableView *)TableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-50)];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (void)viewDidLoad {
    self.navigationItem.title = @"确认订单";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [super viewDidLoad];
    
    [self TableView];
    [self headerUIView];
}
- (void)headerUIView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(8, 25, 30, 30)];
    image.image = [UIImage imageNamed:@"icon_location"];
    [view addSubview:image];
    UIImageView *imageyou = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-30, 27.5, 25, 25)];
    imageyou.image = [UIImage imageNamed:@"iconfont-arrow"];
    [view addSubview:imageyou];
    self.tableView.tableHeaderView = view;

    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(45, 15, 100, 20)];
    name.text = @"雷 先生";
    name.font = [UIFont systemFontOfSize:14];
    name.textColor = UIColorFromRGB(0x333333);
    CGSize maximumLabelSize = CGSizeMake(100, 20);
    CGSize expectSize = [name sizeThatFits:maximumLabelSize];
    name.frame = CGRectMake(40, 16, expectSize.width, expectSize.height);
    [view addSubview:name];

    UILabel *phonelab = [[UILabel alloc]initWithFrame:CGRectMake(name.size.width+55, 15, 100, 20)];
    phonelab.text = @"13693619838";
    phonelab.font = [UIFont systemFontOfSize:15];
    phonelab.textColor = UIColorFromRGB(0x333333);
    [view addSubview:phonelab];
    
    UILabel *addressLab = [[UILabel alloc]initWithFrame:CGRectMake(40, 35, WIDTH-80, 40)];
    addressLab.text = @"济南市历下区山大路解放路华强电子广场B栋2308";
    addressLab.font = [UIFont systemFontOfSize:12];
    addressLab.numberOfLines = 0;
    addressLab.textColor = UIColorFromRGB(0x666666);
    [view addSubview:addressLab];
    
    
    
    //
    UIView *viewal = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-45-63, WIDTH, 45)];
    viewal.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewal];
    UIView *xian = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    xian.backgroundColor = UIColorFromRGB(0xefefef);
    [viewal addSubview:xian];
    UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(8, 12, 110, 20)];
    labe.text = @"还需要支付:";
    labe.textColor = UIColorFromRGB(0x666666);
    [viewal addSubview:labe];
    
    _monlab = [[UILabel alloc]initWithFrame:CGRectMake(110, 12, 200, 20)];
    _monlab.textColor = UIColorFromRGB(0xE41D24);
    [viewal addSubview:_monlab];
    
    UIButton *monBut = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH - 88, 8, 80, 30)];
    [monBut setTitle:@"付款"forState:UIControlStateNormal];
    monBut.titleLabel.font = [UIFont systemFontOfSize:15];
    monBut.layer.cornerRadius = 8;
    [monBut setBackgroundColor:[UIColor redColor]];
    [monBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [monBut addTarget:self action:@selector(butGo:) forControlEvents:UIControlEventTouchUpInside];
    [viewal addSubview:monBut];

}
- (void)butGo:(UIButton *)but{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 1;
    }else if (section == 3){
        return _shopArr.count+2;
    }
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell = [[ShoppingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0) {
        
        NSArray *arr = @[@"在线支付",@"货到付款"];
        cell.title.text = arr[indexPath.row];

        [cell.songbut addTarget:self action:@selector(butzhifu:) forControlEvents:UIControlEventTouchUpInside];
        cell.songbut.tag = indexPath.row+100;
        if (z==indexPath.row) {
            [cell.songbut setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
        }else{
            [cell.songbut setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
        }
    }else if (indexPath.section == 1){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        if (indexPath.row == 0) {
            cell.title.text = @"送达时间";
            if (_timestr) {
                cell.viewlab.text = _timestr;
            }else{
                _timestr = @"立即送达";
                cell.viewlab.text = _timestr;
            }

            [cell.viewlab setTextAlignment:2];
        }else{
            cell.title.text = @"备注";
            if (_remarklab.length == 0) {
                _remarklab = @"口味,偏好等其他需求";
            }
            cell.viewlab.text = _remarklab;

            cell.viewlab.textAlignment = 2;

        }
    }else if (indexPath.section == 2){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.title.text = @"优惠券";
        cell.viewlab.text = @"没有可用的优惠券";
        cell.viewlab.textAlignment = 2;

    }else if (indexPath.section == 3){
        
        if (indexPath.row < _shopArr.count) {
            cell.title.text = [NSString stringWithFormat:@"%@",_shopArr[indexPath.row][@"name"]];
            cell.title.textColor = UIColorFromRGB(0x666666);
            cell.title.font = [UIFont systemFontOfSize:13];
            
            cell.price.text = [NSString stringWithFormat:@"¥%@",_shopArr[indexPath.row][@"min_price"]];
            cell.price.textColor = UIColorFromRGB(0x666666);
            cell.price.font = [UIFont systemFontOfSize:13];
            
            cell.numer.text = [NSString stringWithFormat:@"x%@",_shopArr[indexPath.row][@"orderCount"]];
            cell.numer.textColor = UIColorFromRGB(0x666666);
            cell.numer.font = [UIFont systemFontOfSize:13];
        }else if (indexPath.row == _shopArr.count){
            cell.title.text = @"餐盒费";
            cell.title.textColor = UIColorFromRGB(0x666666);
            cell.title.font = [UIFont systemFontOfSize:13];
            
            cell.price.text = @"¥5";
            cell.price.textColor = UIColorFromRGB(0x666666);
            cell.price.font = [UIFont systemFontOfSize:13];
        }else if (indexPath.row == _shopArr.count+1){
            cell.title.text = @"配送费";
            cell.title.textColor = UIColorFromRGB(0x666666);
            cell.title.font = [UIFont systemFontOfSize:13];
            
            cell.price.text = @"¥10";
            cell.price.textColor = UIColorFromRGB(0x666666);
            cell.price.font = [UIFont systemFontOfSize:13];
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UIButton*but=(UIButton*)[self.view viewWithTag:indexPath.row+100];
        for (int i=0; i<2; i++) {
            UIButton*button=(UIButton*)[self.view viewWithTag:i+100];
            [button setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
        }
        z = but.tag-100;
        [but setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    }else if (indexPath.section == 1 && indexPath.row == 0){
        if (_pickview == nil) {
            _pickview=[[ZHPickView alloc] initPickviewWithArray:[self orderTime] isHaveNavControler:NO];
            _pickview.delegate=self;
            
        }
        UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-200)];
        [but addTarget:self action:@selector(remPickview:) forControlEvents:UIControlEventTouchUpInside];
        but.tag = 12138;
        [self.view addSubview:but];
        
        [_pickview show];
    }else if (indexPath.section == 1 && indexPath.row == 1){
        
        RemarkViewController *rvc = [[RemarkViewController alloc]init];
        if (![_remarklab isEqualToString:@"口味,偏好等其他需求"]) {
            rvc.str = _remarklab;
        }
        void(^aBlock)(NSString *str) = ^(NSString *str){
            _remarklab = str;
            [_tableView reloadData];
        };
        rvc.block = aBlock;
        
        [self.navigationController pushViewController:rvc animated:YES];
    }
}
- (void)remPickview:(UIButton *)but{
    [_pickview remove];
    [but removeFromSuperview];
}
//配送时间计算
- (NSMutableArray *)orderTime{
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
    [formatter2 setDateFormat:@"HH:mm"];
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    //当前时间
    NSDate *nowDate = [NSDate date];
    //配送时间
    NSTimeInterval  intervall1 =10   *60*1;
    
    nowDate = [nowDate initWithTimeInterval:intervall1 sinceDate:nowDate];
    
    NSInteger interval1 = [zone secondsFromGMTForDate: nowDate];
    NSString *nowstr = [NSString stringWithFormat:@"%@",[formatter2 stringFromDate:nowDate]];
    nowDate = [formatter2 dateFromString:nowstr];
    nowDate = [nowDate  dateByAddingTimeInterval: interval1];
    NSTimeInterval _fitstDate = [nowDate timeIntervalSince1970]*1;
    
    //店铺最晚时间
    NSDate *endDate = [[NSDate alloc]init];
    NSInteger interval = [zone secondsFromGMTForDate: endDate];
    endDate = [formatter2 dateFromString:@"23:00"];
    NSDate *localDate = [endDate  dateByAddingTimeInterval: interval];
    
    NSTimeInterval _secondDate = [localDate timeIntervalSince1970]*1;
    
    NSTimeInterval  intervall =30*60*1;
    NSMutableArray *timear = [[NSMutableArray alloc]init];
    while (_fitstDate < _secondDate) {
        localDate = [localDate initWithTimeInterval:-intervall sinceDate:localDate];
        _secondDate = [localDate timeIntervalSince1970]*1;
        
        NSString *str2 = [NSString stringWithFormat:@"%@",[formatter2 stringFromDate:endDate]];
        
        endDate = [endDate initWithTimeInterval:-intervall sinceDate:endDate];
        NSString *str = [NSString stringWithFormat:@"%@",[formatter2 stringFromDate:endDate]];
        [timear addObject:[NSString stringWithFormat:@"%@~%@",str,str2]];
    }
    
    [timear removeLastObject];
    
    [arr addObject:@"尽快送达"];
    
    for (long i = timear.count;i>0;i--) {
        [arr addObject:timear[i-1]];
    }
    return arr;
}

//支付方式
- (void)butzhifu:(UIButton *)but{
    
    for (int i=0; i<2; i++) {
        UIButton*button=(UIButton*)[self.view viewWithTag:i+100];
        [button setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    }
    z = but.tag-100;
    [but setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 15)];
    header.backgroundColor = UIColorFromRGB(0xefefef);
    return header;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 40;
    
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}
#pragma mark ZhpickVIewDelegate

-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    UIButton *but = (id)[self.view viewWithTag:12138];
    [but removeFromSuperview];
    _timestr = resultString;
    [_tableView reloadData];
}





-(void)back{
    [_pickview remove];

    [self.navigationController popViewControllerAnimated:YES];
}
@end
